package equipments;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import java.util.List;


import systems.database.DataBaseHandler;
import systems.languages.MultiLanguages;
import tools.BonusAttribut;
import tools.Visitable;
import tools.Visitor;

public class Armor implements Items, Visitable {

  private int id = 0;
  private String name;
  private int level;
  private int nbBonusAttr;
  private int nbManastone;
  private boolean hasDivineSlot;

  private int physical_def;
  private int magical_resist;
  private int evasion;
  private int block;
  private List<BonusAttribut> bonusList;

  public Armor(){
    this.nbBonusAttr=0;
    this.setBonusList(new LinkedList<BonusAttribut>());
  }

  public void init(String id)
  {
    this.id = Integer.valueOf(id);
    this.initItem(DataBaseHandler.getStatement());
  }

  @Override
  public int getLevel() {
    return this.level;
  }

  @Override
  public String getName() {
    return this.name;
  }

  @Override
  public int getNbBonusAttr() {
    return this.nbBonusAttr;
  }

  @Override
  public int getNbManastone() {
    return this.nbManastone;
  }

  @Override
  public boolean hasDivineSlot() {
    return this.hasDivineSlot;
  }

  @Override
  public void initItem(Statement transmission) {
    ResultSet resultat;
    try {

      //Starting with getting the name ...
      resultat= transmission.executeQuery("SELECT body FROM "
          +MultiLanguages.getProperties("database.Language.name")
          +" where oid = "+this.id);
      while(resultat.next())
        this.name=resultat.getString("body");

      //Recuperation des stats des initialisation
      resultat = transmission.executeQuery("Select * from items where id="+id);
      while(resultat.next())
      {
        int cpt=1;
        while(cpt<13)
        {
          if(resultat.getString("bonus_attr"+cpt)!=null)
          {     
        	  this.getBonusList().add(new BonusAttribut(resultat.getString("bonus_attr"+cpt)));
        	  this.nbBonusAttr++;
   		  }
          cpt++;
        }
        //J'ai pas réussi a trouver d'arme qui n'avait pas de divine slot...
        this.hasDivineSlot=true;

        this.level = resultat.getInt("level");

        this.setPhysical_def(resultat.getInt("physical_defend"));
        this.setMagical_resist(resultat.getInt("magical_resist"));
        this.setEvasion(resultat.getInt("dodge"));
        this.setBlock(resultat.getInt("block"));
        this.nbManastone=resultat.getInt("slots");
/*
        if(this.nbBonusAttr!=0)
          for(int i=1;i<this.nbBonusAttr+1;i++)
          {
        	  System.out.println(" bonus :"+resultat.getString("bonus_attr"+i));
            this.getBonusList().add(new BonusAttribut(resultat.getString("bonus_attr"+i)));
      }*/
      }
    } catch (SQLException e) {
      e.printStackTrace();
    }
    //	System.out.println(this.toString());
  }

  public String toString()
  {
    String bonus="";
    for(int i=0;i<this.getBonusList().size();i++)
    {
      bonus+="\nBonus "+i+": "+this.getBonusList().get(i).toString();
    }

    return "Name :"+ this.name+
    "\nlevel: "+this.level+
    "\nphysical_def: "+this.getPhysical_def()+
    "\nmagical_resist: "+this.getMagical_resist()+
    "\nManastone slot: "+this.nbManastone+
    //	"\nGodstone slot: "+this.hasDivineSlot+
    "\nevasion: "+this.getEvasion()+
    "\nblock: "+this.getBlock()+
    bonus;
  }


  public void setPhysical_def(int physical_def) {
    this.physical_def = physical_def;
  }


  public int getPhysical_def() {
    return physical_def;
  }


  public void setMagical_resist(int magical_resist) {
    this.magical_resist = magical_resist;
  }


  public int getMagical_resist() {
    return magical_resist;
  }


  public void setEvasion(int evasion) {
    this.evasion = evasion;
  }


  public int getEvasion() {
    return evasion;
  }


  public void setBlock(int block) {
    this.block = block;
  }


  public int getBlock() {
    return block;
  }


  public void setBonusList(List<BonusAttribut> bonusList) {
    this.bonusList = bonusList;
  }


  public List<BonusAttribut> getBonusList() {
    return bonusList;
  }


  @Override
  public void accept(Visitor visitor) {
    visitor.visit(this);
  }

  /**
   * This function return true if the armor can be used as a shield.
   * @return true if the armor can be used as a shield, else false.
   */
  public boolean isShield()
  {
    return (this.physical_def==0)?true:false;
  }


  @Override
  public int getID() {
    return this.id;
  }

  
  public void updateName() {
    ResultSet resultat;
    try {
      resultat= DataBaseHandler.getStatement().executeQuery("SELECT body FROM "
          +MultiLanguages.getProperties("database.Language.name")
          +" where oid = "+this.id);
      while(resultat.next())
        this.name=resultat.getString("body");  
    } catch (SQLException e) {
      e.printStackTrace();
    }
  }

public void acceptBonusRemoval(Visitor visitor) {
	visitor.bonusvisit(this);		
}
}
